*** Calculate number of children for single household ***

do "E:\yungyu\lottery_baby\do\set_environment.do"

capture log close
log using "$log\1.1.childnum_individual.log" , replace
clear
set more off

foreach x in "fr" "mo"{ //separate loop for father and mother
	use "$public\real_jhq.dta" , clear

	keep if gender=="1" | gender=="2" //Keep only male and female
	drop if born_year==. //drop children with unknown born year
	drop if born_year>109 //drop children born after 2020 (ROC year 109)

	rename `x'_idn pers_idn
	drop if pers_idn==""

	gen baby = 1

	replace born_year = 92 if born_year<=92 //children born before 2003 (ROC year 92)

	collapse (sum)baby , by(pers_idn gender born_year) //calculate total number of children each birth year by each parent

	replace gender = "boy"  if gender=="1"
	replace gender = "girl" if gender=="2"

	reshape wide baby , i(pers_idn born_year) j(gender) string

	reshape wide baby* , i(pers_idn) j(born_year)
	rename babyboy92 prebabyboy //children born before 2003 (ROC year 92)
	rename babygirl92 prebabygirl

	mvencode baby* , mv(0) //recode missing as 0

	save "$wdata\childnum_`x'.dta", replace
}

use "$wdata\childnum_fr.dta", clear
ap using "$wdata\childnum_mo.dta"

rename pers_idn idn

collapse (sum)pre* baby* , by(idn)

merge 1:1 idn using "$public\real_jhq.dta"
drop if _m==1 //drop parent who are not citizens

keep idn pre* baby*
order idn pre* baby*

reshape long babyboy babygirl , i(idn pre*) j(year)

sort idn year
foreach x in "boy" "girl"{ //calculate culmulative number of children each year
	replace prebaby`x'=prebaby`x'[_n-1]+baby`x'[_n-1] if idn==idn[_n-1]
	gen totbaby`x'=prebaby`x'+baby`x'
}

save "$wdata\childnum.dta" , replace

forv i = 93(1)109{ //Year 2004 to 2020
	use "$wdata\childnum.dta" , clear
	keep if year == `i'
	save "$wdata\childnum_`i'.dta" , replace
}

log close
